<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\web\controller;
use xsocketlog\socketlog;
class File extends Base{

    public function upload_picture_base64(){
        if(IS_POST){
            $image_base64 = input('image');

            if(!empty($image_base64)){
                $data = [
                    'type'=> 'other',
                    'uid'=>1
                ];

                $data = array_merge($data, $_POST);

                $file_name = '/tmp/base64image_'.md5(date('YmdHis').rand(0,6666));

                if(file_exists($file_name)){
                    unlink($file_name);
                }
                $pos = strpos($image_base64,'base64,') + 7;

                $image_base64 = substr($image_base64,$pos, strlen($image_base64)-$pos);

                file_put_contents($file_name,base64_decode($image_base64));

                $filename = $this->compress($file_name);
                if($filename===false) $this->apiReturnErr('未知错误，上传图片不存在');
                $file = [
                    'image' => new \CURLFile($file_name,'image/jpeg','tmp.jpg')
                ];
                $data = array_merge($data, $file);

                $ch = curl_init();
                $url = config('API_URL').'/file/upload';

                curl_setopt ( $ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT,5);
                curl_setopt ( $ch, CURLOPT_POST, 1);
                curl_setopt ( $ch, CURLOPT_HEADER, 0);
                curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
                $out_put = curl_exec ($ch);
                curl_close($ch);

                $ret = json_decode($out_put,1);

                if(!is_array($ret)){
                    $ret = json_decode($ret,1);
                }
                unlink($file_name);

                if($ret['code']!=0){
                    $this->apiReturnErr($ret['data']);
                }else{
                    $this->apiReturnSuc($ret['data']);
                }
            }


        }


    }

    /**
     * 判断并压缩500K以上图片
     */
    protected function compress($filename)
    {
        function check_gifcartoon($image_file){
            $fp = fopen($image_file,'rb');
            $image_head = fread($fp,1024);
            fclose($fp);
            return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
        }

        /**
         * desription 压缩图片
         * @param string $imgsrc 图片路径
         * @param string $imgdst 压缩后保存路径
         */
        function compressed_image($imgsrc,$imgdst,$maxWidth = 960, $maxHeight = 720){
            list($width,$height,$type)=getimagesize($imgsrc);
            $new_width = $width>$maxWidth?$maxWidth:$width;
            $new_height = $height>$maxHeight?$maxHeight:$height;
            switch($type){
                case 1:
                    $giftype=check_gifcartoon($imgsrc);
                    if($giftype){
                        header('Content-Type:image/gif');
                        $image_wp=imagecreatetruecolor($new_width, $new_height);
                        $image = imagecreatefromgif($imgsrc);
                        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                        //75代表的是质量、压缩图片容量大小
                        imagejpeg($image_wp, $imgdst,75);
                        imagedestroy($image_wp);
                    }
                    break;
                case 2:
                    header('Content-Type:image/jpeg');
                    $image_wp=imagecreatetruecolor($new_width, $new_height);
                    $image = imagecreatefromjpeg($imgsrc);
                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //75代表的是质量、压缩图片容量大小
                    imagejpeg($image_wp, $imgdst,75);
                    imagedestroy($image_wp);
                    break;
                case 3:
                    header('Content-Type:image/png');
                    $image_wp=imagecreatetruecolor($new_width, $new_height);
                    $image = imagecreatefrompng($imgsrc);
                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //75代表的是质量、压缩图片容量大小
                    imagejpeg($image_wp, $imgdst,75);
                    imagedestroy($image_wp);
                    break;
            }
        }
        if(is_file($filename)){
            $size = filesize($filename);
            if($size>500*1024){
                $new_name = $filename.'.cp';
                compressed_image($filename, $filename.'.cp');
                return $new_name;
            }else{
                return $filename;
            }
        }else{
            return false;
        }

    }

}
